#ifndef _COMMON_H
#define _COMMON_H

#define CLIPORT 7988
#define CHKPORT 7989
#define MAGIC_NUMBER 0x46716236

#define DEFAULT_CHUNK_SIZE 64*1024*1024
#define DEFAULT_REPLICA 3

enum CH_CMD
{
    CH_CLIENT_CREATE_FILE,
    CH_CLIENT_UPLOAD_FILE,
    CH_CLIENT_DELETE_FIEL,
    CH_CLIENT_DOWNLOAD_FILE,
    CH_CHUNKER_REGISTER,
    CH_CHUNKER_HEARTBEAT,
    CH_CHUNKER_SYNCHK
};
enum CH_STAT
{
    CH_TREACKER_PRAEPEARE,
    CH_TRACKER_WAIT_HEADER,
    CH_TRACKER_WAIT_DATA,
    CH_TRACKER_HANDLE
};

struct cadoop_header
{
    unsigned int magic;
    unsigned char type;
    unsigned char cmd;
    unsigned short checksum;
    unsigned int payload;
};


struct dfs_metadata
{
    unsigned int version;
    unsigned int filesize;
    unsigned int fsnamelen;
    char filename[0];
};

enum storage_stat
{
    STORAGE_JOIN,
    STORAGE_SYNC,
    STORAGE_MEMBER,
    STORAGE_UPDATA,
    STORAGE_NORMAL,
    STORAGE_DEAD
};

struct storage
{
    int storageid;
    char ip[16];
    unsigned short port;
    unsigned long total;
    unsigned long avail;
    unsigned long used;
    enum storage_stat stat;
};

struct chunk {
    int chunkid;
    time_t timestamp;
    int storageid;
    int size;
};

struct dfs_file
{
    char name[128];
    unsigned long size;
    int chunk_size;
    int replica;
    struct chunk* chunks;
};

struct chunker_register_cmd
{
    unsigned int storageid;
    char ip[16];
    unsigned long total;
    unsigned long avail;
    unsigned long used;
    unsigned short port;
};

struct client_getloc_cmd
{
    int  magic;
    int  version;
    char clientid[32];
    char filename[32];
    int  chunknum;
    int  excludenum;
    int  excludeid[0];
};

struct tracker_getloc_response
{
    int magic;
    int version;
    int result;
    int chunknum;
    int storageid[0];
};

struct client_upload_cmd
{
    int magic;
    int version;
    char chunkname[64];
    int replica;
    int storageid[0];
      
};

#endif
